<template>
  <el-dialog title="银行配置" :visible.sync="visible" @close="emptyData">
    <el-form :model="form" label-width="80px">
      <el-form-item label="银行类型">
        <el-select v-model="form.code" filterable placeholder="请选择银行" style="width: 100%">
          <el-option v-for="(item, index) in banks" :label="item.code + '-' + item.name" :value="item.code"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="协议类型">
        <el-radio v-model="form.type" label="socket">Socket</el-radio>
        <el-radio v-model="form.type" label="http">Http</el-radio>
      </el-form-item>
      <el-form-item label="URL" v-if="form.type === 'http'">
        <el-input v-model="form.url" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="端口">
        <el-input v-model="form.port" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="字符集">
        <el-input v-model="form.charset" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="JS脚本" placeholder="JS脚本的目录">
        <el-input v-model="form.path" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="submit" :loading="loading">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      loading: false,
      banks: [],
      form: {
        id: null,
        name: '',
        type: '',
        code: '',
        port: 0,
        charset: 'UTF-8',
        url: '',
      },
    }
  },
  watch: {
    // 获取vuex中的screenObj对象，进行监听
    visible(val) {
      if (val) {
        this.fetchBankData()
      }
    }
  },
  mounted() {
  },
  created() {
  },
  methods: {
    show(data) {
      if (data) {
        this.form = data
      }
      this.visible = true
    },
    fetchBankData() {
      const _this = this;
      request({
        method: 'get',
        url: '/bank/banks',
      }).then(function (res) {
        if (res.status === 200) {
          _this.banks = res.data
        }
      })
    },
    submit() {
      let _this = this
      _this.loading = true
      request({
        method: 'post',
        url: '/bank/saveUpdate',
        data: this.form,
      }).then(function (res) {
        _this.loading = false
        _this.visible = false
        if (res.data.code === 1) {
          _this.$message({ message: '操作成功', type: 'success' });
          _this.$emit('ok')
        } else {
          this.$notify.error({ title: '操作失败', message: res.data.msg });
        }
      })
    },
    cancel() {
      this.visible = false
      this.emptyData()
    },
    emptyData() {
      this.form.id = null;
      this.form.name = '';
      this.form.code = '';
      this.form.port = 0;
      this.form.type = '';
      this.form.url = '';
      this.form.charset = 'UTF-8';

    }
  }
}
</script>
